<template>
  <div id="customer">
    <el-container>
      <el-header>
        <el-row>
          <el-col :span="2"><div class="grid-content bg-purple">
            <el-button type="primary" @click="addCustomer">添加</el-button>
          </div></el-col>
          <el-col :span="6"><div class="grid-content bg-purple-light">
            <el-input
              v-model="input"
              placeholder="请输入内容"
              clearable
            />
          </div></el-col>
          <el-col :span="6"><div class="grid-content bg-purple">
            <el-button class="btn-search2" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
          </div></el-col>
          <el-col :span="10"><div class="grid-content bg-purple-light" /></el-col>
        </el-row>
      </el-header>

      <el-main>
        <el-table
          :data="customers"
          stripe
          style="width: 100%"
        >
          <el-table-column
            prop="id"
            label="编号"
            width="180"
            align="center"
          />
          <el-table-column
            prop="realname"
            label="姓名"
            width="180"
            align="center"
          />
          <el-table-column
            prop="telephone"
            label="手机号"
            align="center"
          />
          <el-table-column
            prop="status"
            label="状态"
            align="center"
          />
          <el-table-column
            prop="operation"
            label="操作"
            align="center"
          >
            <template slot-scope="scope">
              <a @click="deleteById(scope.row,scope.$index)">删除</a>  &nbsp;&nbsp;&nbsp;&nbsp;
              <a @click="editCustomer(scope.row)">编辑</a>  &nbsp;&nbsp;&nbsp;&nbsp;
              <a @click="details(scope.row.id)">详情</a>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer>
        <!-- 底部分页 -->
        <div class="block">
          <el-pagination
            :current-page.sync="currentPage1"
            :page-size="5"
            layout="total, prev, pager, next"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-footer>

      <!-- 添加/删除的模态框 -->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form ref="dialogform" :model="form" :rules="rules">
          <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
            <el-input v-model="form.username" autocomplete="off" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item label="姓名" :label-width="formLabelWidth" prop="realname">
            <el-input v-model="form.realname" autocomplete="off" placeholder="请输入姓名" />
          </el-form-item>
          <el-form-item label="手机号" :label-width="formLabelWidth" prop="telephone">
            <el-input v-model="form.telephone" autocomplete="off" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
            <el-input v-model="form.password" autocomplete="off" placeholder="请输入密码" />
          </el-form-item>
          <el-form-item label="状态" :label-width="formLabelWidth" prop="status">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option label="禁用" value="shanghai" />
              <el-option label="启用" value="beijing" />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm('dialogform')">确 定</el-button>
        </div>
      </el-dialog>
    </el-container>
  </div>
</template>

<script>
import qs from 'querystring'

export default {
  data() {
    return {
      value: true,
      customers: [],
      input: '',
      total: 0,
      currentPage1: 1,
      pageSize: 5,
      // 模态框
      title: '添加顾客信息',
      dialogFormVisible: false,
      form: {
        username: '',
        realname: '',
        telephone: '',
        password: '',
        delivery: false,
        type: [],
        status: '',
        desc: ''
      },
      formLabelWidth: '120px',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, max: 15, message: '请输入长度在 1 到 15 个字符', trigger: 'blur' }
        ],
        realname: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 15, message: '请长度在 1 到 15 个字符', trigger: 'blur' }
        ],
        telephone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { min: 3, max: 15, message: '请长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '请长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    customerList() {
    // this.customers-所有产品
    // 查询所有customers
    // 每一个产品id匹配所有customers，匹配成功后将当前customers的name->当前产品
    }
  },
  created() {
  // this.$axios.get('/customer/findAll').then(response=>{
  //   console.log(response);
  //   this.categorys = response.data.data;
  // })
    this.findAll()
  },
  methods: {
  // 给确定按钮绑定事件--提交表单
    submitForm(formName) {
      var self = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
        // 表单验证成功-获取用户输入信息->发送保存请求
          self.$axios.post('/customer/saveOrUpdate', qs.stringify(self.form)).then(response => {
            if (response.data.status == 200) {
            // 保存成功
            // 1，关闭模态框
            // 2，刷新页面
              self.findAll()
              // 3，3提示用户保存成功-弹框
              this.$message({
                message: '成功保存',
                type: 'success'
              })
            } else {
            // 保存失败
              this.$message({
                message: '成功失败',
                type: 'error'
              })
            }
            self.dialogFormVisible = false
          })
        } else {
        // 表单验证失败
          console.log('error submit!')
          return false
        }
      })
    },
    // 给添加按钮绑定事件
    addCustomer() {
    // 1，显示模态框
      this.dialogFormVisible = true
      this.title = '添加顾客信息'
      this.form = {}
    },
    // 给搜索按钮绑定事件
    search() {
    // 1,获取用户输入的内容
      if (this.input) {
      // 2,发送请求->给表格展现
        this.findAll(this.input)
      } else {
        this.findAll()
      }
    },
    // 封装删除函数——单个删除
    deleteById(row, index) {
      var self = this // 避免this指向混乱
      var id = row.id
      // 点击删除弹出提示框
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送删除请求
        this.$axios.get('/customer/deleteById', { params: { id }}).then(response => {
          if (response.data.status == 200) {
            // 删除成功，刷新页面
            self.findAll()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          } else {
            // 删除失败
            this.$message.error('删除失败')
            // alert(response.data.message);
          }
        })
      }).catch(() => {
        // 用户取消删除
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 给编辑按钮绑定事件
    editCustomer(row) {
    // 1,打开模态框
      this.dialogFormVisible = true
      this.title = '修改顾客信息'
      this.form = {
        id: row.id,
        username: row.username,
        realname: row.realname,
        telephone: row.telephone,
        password: row.password,
        status: row.status
      }
    },
    // 给详情按钮绑定事件
    details(id) {
    // alert(id);
      this.$router.push({ path: '/details/index', query: { id: id }})
    },
    // 封装全局顾客分页请求
    findAll(realname) {
      if (realname) {
      // 搜索调用
        var data = {
          page: this.currentPage1,
          pageSize: this.pageSize,
          realname: realname
        }
      } else {
      // 非搜索调用
        var data = {
          page: this.currentPage1,
          pageSize: this.pageSize
        }
      }

      this.$axios.post('/customer/query', qs.stringify(data)).then(response => {
        this.customers = response.data.data.list
        this.total = response.data.data.total
      // console.log(this.customers);
      // var customers = response.data.data.list.id;
      // for(var i=customers;i>0;i--){}
      })
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.currentPage1 = val
      this.findAll()
    }

  }
}
</script>

<style lang="scss" scoped>
// 主体
.el-header, .el-footer {
    // background-color: #B3C0D1;
    // color: #333;
    // text-align: center;
    line-height: 60px;
  }

  .el-main {
    background-color: #f0f7f8;
    text-align: center;
    line-height: 30px;

    a{
      color: blue;
  }
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }

  //头部
  .el-row {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-col {
    border-radius: 4px;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .btn-search2{
    margin-left: 10px;
  }

</style>
